package com.lenovo.agricultureclient.ui.fragment;

import java.util.ArrayList;

import com.lenovo.agricultureclient.R;
import com.lenovo.agricultureclient.bean.SensorBean;
import com.lenovo.agricultureclient.bean.SensorConfig;
import com.lenovo.agricultureclient.bean.SensorValue;
import com.lenovo.agricultureclient.ui.activity.RealTimeDataActivity;
import com.lenovo.agricultureclient.ui.adapter.SensorGridAdapter;

import android.R.integer;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
/**
 * 环境指标子窗体
 * @author zongbingwu
 *
 */
public class EnvirFragment extends AppBaseFragment
{
	public static final String TAG = "EnvirFragment";
	//所有传感器数据的集合
	private ArrayList<SensorBean> mSensorList;
	//传感器数据值适配器
	private SensorGridAdapter mAdp;
	//存放所有传感器view的容器
	private GridView mGridView;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mSensorList = new ArrayList<SensorBean>();
		
		mSensorList.add(new SensorBean(getString(R.string.pm25_title))); //PM2.5
		mSensorList.add(new SensorBean(getString(R.string.CO2_title))); //C02浓度
		mSensorList.add(new SensorBean(getString(R.string.light_title))); //光照强度
		mSensorList.add(new SensorBean(getString(R.string.air_tmper_title))); //空气温度
		mSensorList.add(new SensorBean(getString(R.string.air_humid_title))); //空气湿度
		mSensorList.add(new SensorBean(getString(R.string.soil_tmper_title))); //土壤温度
		mSensorList.add(new SensorBean(getString(R.string.soil_humid_title))); //土壤湿度
		
		mAdp = new SensorGridAdapter(getActivity(), mSensorList);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		return inflater.inflate(R.layout.envir_fragment, container, false);
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) 
	{
		super.onActivityCreated(savedInstanceState);
		
		mGridView = (GridView)getView().findViewById(R.id.sensor_grid_view);
		mGridView.setAdapter(mAdp);
		//设置grid view的点击事情，点到传感器以后再跳转到该传感器的实时曲线图表界面
		mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				int chartItem = RealTimeDataActivity.PM25_CHART;
				switch(position)
				{
				case 0:
					chartItem = RealTimeDataActivity.PM25_CHART;
					break;
				case 1:
					chartItem = RealTimeDataActivity.CO2_CHART;
					break;
				case 2:
					chartItem = RealTimeDataActivity.LIGHT_CHART;
					break;
				case 3:
					chartItem = RealTimeDataActivity.AIR_T_CHART;
					break;
				case 4:
					chartItem = RealTimeDataActivity.AIR_H_CHART;
					break;
				case 5:
					chartItem = RealTimeDataActivity.SOIL_T_CHART;
					break;
				case 6:
					chartItem = RealTimeDataActivity.SOIL_H_CHART;
					break;
				}
				//跳转到指定传感器的实时曲线图表界面
				Intent intent = new Intent(getActivity(), RealTimeDataActivity.class);
				intent.putExtra(RealTimeDataActivity.CHART_TYPE, chartItem);
				startActivity(intent);
			}
		});
		updateView();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	@Override
	public void onDestroyView() {
		super.onDestroyView();
	}
	
	//刷新环境指标子窗体界面
	public void updateView()
	{
		SensorValue value = mApp.getCurSensorValue();
		SensorConfig config = mApp.getSensorConfig();
		if(value!=null && config!=null)
		{
			//PM2.5
			SensorBean pm25 = mSensorList.get(0);
			pm25.setValue(value.getPm25());
			pm25.setMaxValue(config.maxPM25);
			
			//C02浓度
			SensorBean co2 = mSensorList.get(1);
			co2.setValue(value.getCo2());
			co2.setMinValue(config.minCo2);
			co2.setMaxValue(config.maxCo2);
			
			//光照强度
			SensorBean light = mSensorList.get(2);
			light.setValue(value.getLight());
			light.setMinValue(config.minLight);
			light.setMaxValue(config.maxLight);
			
			//空气温度
			SensorBean airT = mSensorList.get(3);
			airT.setValue(value.getAirTemper());
			airT.setMinValue(config.minAirTemperature);
			airT.setMaxValue(config.maxAirTemperature);
			
			//空气湿度
			SensorBean airH = mSensorList.get(4);
			airH.setValue(value.getAirHumid());
			airH.setMinValue(config.minAirHumidity);
			airH.setMaxValue(config.maxAirHumidity);
			
			//土壤温度
			SensorBean soilT = mSensorList.get(5);
			soilT.setValue(value.getSoilTemper());
			soilT.setMinValue(config.minSoilTemperature);
			soilT.setMaxValue(config.maxSoilTemperature);
			
			//土壤湿度
			SensorBean soilH = mSensorList.get(6);
			soilH.setValue(value.getSoilHumid());
			soilH.setMinValue(config.minSoilHumidity);
			soilH.setMaxValue(config.maxSoilHumidity);
			
			mAdp.notifyDataSetChanged();
		}
	}
}
